package com.tjtt.tdm.fileupload.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.fileupload.LogPrinter;
import com.tjtt.tdm.fileupload.bean.TdmAttachment;
import com.tjtt.tdm.fileupload.service.TdmAttachmentService;
import com.tjtt.tdm.system.service.LogService;

public class TdmAttachmentAction extends BaseAction {

	private TdmAttachmentService tdmAttachmentService;
	private LogService logService;
 
	
	public LogService getLogService() {
		return logService;
	}


	public void setLogService(LogService logService) {
		this.logService = logService;
	}


	public TdmAttachmentService getTdmAttachmentService() {
		return tdmAttachmentService;
	}


	public void setTdmAttachmentService(TdmAttachmentService tdmAttachmentService) {
		this.tdmAttachmentService = tdmAttachmentService;
	}


	public void queryAttachment(){
		try {
			//取出页面中的查询条件
			Map<String, String> filterMap = getPagerMap();
			String code_id = getRequest().getParameter("code_id");
			String column = getRequest().getParameter("column");
			filterMap.put("code_id", code_id);
			filterMap.put("column", column);
			String[] viewArray = { "id", "file_name", "file_type","file_version","file_path", "file_desc", "create_time","create_userid","create_username","code_id","file_length"};
			// 查询所有的数据
			PageResult result = tdmAttachmentService.queryAttachment(filterMap);
			String returnStr = getColumnJson(result, viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
	/**
	 * 获取所有对应的附件
	 * @return
	 */
	public void getAttachments() throws IOException{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		try {
			String code_id="";
			if(getRequest().getParameter("code_id")!=null)
			{
				code_id = getRequest().getParameter("code_id");
			}			
			String column = getRequest().getParameter("column");
			if(!code_id.equals("")&&code_id!=null)
			{
				List<TdmAttachment> result = tdmAttachmentService.getAttachment(code_id, column);
				JSONArray jsonObj = JSONArray.fromObject(result);
				out.print(jsonObj.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("error");
		} 
	}
	public void deleteAttachment() throws IOException{
		PrintWriter out = getResponse().getWriter();
		try {
			String id = getRequest().getParameter("id");
			if (id != null && !id.isEmpty()) {
				TdmAttachment attachment = tdmAttachmentService.getOneAttachment(id);
				tdmAttachmentService.deleteAttachment(attachment);
				String ctxDir = getSession().getServletContext()
						.getInitParameter("filePath");
				if (!ctxDir.endsWith(String.valueOf(File.separatorChar))) {
					ctxDir = ctxDir + File.separatorChar;
				}
				String path = ctxDir + attachment.getFilePath();
				LogPrinter.info("删除文件：" + path);
				logService.saveLogInfo(this.getLogBase(), "附件管理", "附件管理",attachment.getFileName() +  "被"+getUserNM()+"删除",4);
				File file = new File(path);
				if (file.exists()) {
					boolean flg = file.delete();
					if (flg) {
						LogPrinter.info("删除文件成功：" + path);
					} else {
						LogPrinter.info("删除文件失败：" + path);
					}
				}
			}
			
			out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
	}
	
}
